
//
//  UIViewAnimationViewController.m
//  iOSHeartAnimation
//
//  Created by ddy on 2018/7/27.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "UIViewAnimationViewController.h"
#define buttonNames @[@"动画1",@"动画2",@"动画3",@"动画4",@"动画5"]

@interface UIViewAnimationViewController ()
/** 红色view */
@property (nonatomic, strong) UIView  *redView;
@end

@implementation UIViewAnimationViewController
/** 红色view */
- (UIView *)redView{
    if(!_redView){
        _redView = [UIView new];
        _redView.layer.cornerRadius = 8.0;
        _redView.layer.masksToBounds = YES;
        _redView.backgroundColor = [UIColor redColor];
    }
    return  _redView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"UIViewAnimation";
    //UIView动画
    [self.view addSubview:self.redView];
    self.redView.frame = CGRectMake(200, 100, 100, 100);
    //按钮
    //创建按钮
    for (int i = 0 ; i < 5 ; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        [btn setTitle:buttonNames[i] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 60*i+150, 100, 50);
        //ExclusiveTouch的作用是：可以达到同一界面上多个控件接受事件时的排他性,从而避免bug。也就是说避免在一个界面上同时点击多个UIButton导致同时响应多个方法。
        btn.exclusiveTouch = YES;
        btn.backgroundColor = [UIColor blueColor];
        [btn addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
    
}
///按钮点击
- (void)ButtonClick:(UIButton *)btn{
    switch (btn.tag) {
        case 0:
            [self UIViewAnimation1];
            break;
        case 1:
            [self UIViewAnimation2];
            break;
        case 2:
            [self UIViewAnimation3];
            break;
        case 3:
            [self UIViewAnimation4];
            break;
        case 4:
            [self UIViewAnimation5];
            break;
            
        default:
            break;
    }
}
- (void)UIViewAnimation1{
    [UIView animateWithDuration:3.0 delay:1.0 options:UIViewAnimationOptionPreferredFramesPerSecond60 animations:^{
        self.redView.transform = CGAffineTransformMakeTranslation(30, 300);
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 animations:^{
            self.redView.transform = CGAffineTransformIdentity;
        }];
    }];
}
- (void)UIViewAnimation2{
    [UIView animateWithDuration:3.0 delay:0.5 usingSpringWithDamping:10 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.redView.transform = CGAffineTransformMakeScale(0.5, 0.5);
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.redView.transform = CGAffineTransformIdentity;
        }];
    }];
}
- (void)UIViewAnimation3{
    [UIView animateWithDuration:3.0 animations:^{
        self.redView.transform = CGAffineTransformMakeRotation(3);
        [UIView animateWithDuration:1.0 animations:^{
            self.redView.transform = CGAffineTransformIdentity;
        }];
    }];
}
- (void)UIViewAnimation4{
    [UIView animateWithDuration:3.0 animations:^{
        self.redView.transform = CGAffineTransformMakeRotation(3);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 animations:^{
            self.redView.transform = CGAffineTransformIdentity;
        }];
    }];
}
- (void)UIViewAnimation5{
    [UIView animateKeyframesWithDuration:3.0 delay:1.0 options:UIViewKeyframeAnimationOptionAutoreverse animations:^{
        self.redView.transform = CGAffineTransformMakeRotation(3);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 animations:^{
            self.redView.transform = CGAffineTransformIdentity;
        }];
    }];
}




@end
